<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once ("secure_area.php");

class Users extends Secure_area {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URLa
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct(){
		parent::__construct();
	}

	public function index(){
		//switch tasks
		$task = isset($_POST['task']) ? strtolower($_POST['task']) : '' ;
		switch($task){
			case 'delete':
				$id =  isset($_POST['checkid']) ? $_POST['checkid'] : null;
				if( !empty( $id )){
					$this->delete($id);
				} else {
					$this->message->set('error','Please select from the list to delete record');
				}
				break;

			default:

				break;
		}

		$this->page();

	}

	function page(){
		$start_row = $this->uri->segment(4);
		$limit = 2;
		if(trim($start_row) == ""){
			$start_row = 0;
		}
		$data['title'] = 'Users Listing';
		//listing
		$this->load->model('Admin_users_model','',TRUE);
		$users_qry = $this->Admin_users_model->listUsers();
		$users_table = $this->table->generate($users_qry);
		$data['users_qry'] = $users_qry;
		$data['data_table'] = $users_table;
		$data['main_content'] = 'admin/users_listing';

		//pagination
		$this->load->library('pagination');
		$config['base_url'] = site_url('admin/users/page');
		$config['total_rows'] = $users_qry->num_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['anchor_class'] = 'class="button pagi" ';
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['users_qry_limited'] = $this->Admin_users_model->listUsers_limited($start_row, $limit);

		$this->load->view('admin/includes/template', $data);
	}

	//create new user
	function create(){
		$data['title'] = 'Add User';
		$data['main_content'] = 'admin/users_form';
		$this->load->view('admin/includes/template', $data);
	}

	//listing of users
	function listing(){
		$this->load->model('Admin_users_model','',TRUE);
		$users_qry = $this->Admin_users_model->listUsers();
		$users_table = $this->table->generate($users_qry);
	}

	//edit data
	function edit(){
		$id = $this->uri->segment(4);
		$this->load->model('Admin_users_model','',TRUE);
		$data['row'] = $this->Admin_users_model->getUser($id)->result();
		$data['title'] = 'Edit User';
		$data['main_content'] = 'admin/users_form';
		$this->load->view('admin/includes/template', $data);
	}

	//update existing data
	function update(){
		$this->load->model('Admin_users_model','',TRUE);
		if($this->Admin_users_model->updateUser( $_POST['userid'], $_POST )){
			$this->message->set('success', 'Successfully Updated User.');
		} else {
			$this->message->set('error', 'Failed to update user!');
		}
		redirect('admin/users','refresh');
	}

	//save new data
	function add(){
		$this->load->model('Admin_users_model','',TRUE);
		if($this->Admin_users_model->addUser( $_POST )){
			//send email to user
//			$email_data = null;
//			$email_data['send_to'] = 1;
//			$this->send_email($email_data);
			$this->message->set('success', array('Successfully Created User!', 'Email is sent to user.'));

		} else {
			$this->message->set('error', 'Failed to create user!');
		}
		redirect('admin/users','refresh');
	}

	//delete data
	function delete( $id=array() ){
		$this->load->model('Admin_users_model','',TRUE);
		if( $this->Admin_users_model->deleteUser( $id ) ){
			$this->message->set('success','Successfully deleted user!');
		} else {
			$this->message->set('error','Failed to delete user!');
		}
		return true;
	}

	//send email
	function send_email($data){
		$this->load->helper('phpmailer');
		$send_to = $member_to->email;
		$sender = $this->member->email;
		$subject = ucfirst( $this->member->username ).' messages you in chat.';
		$body = $this->load->view('template_email_view', $data, TRUE);
		send_email($send_to, $sender, $subject, $body);
	}


}
